<?php

// @formatter:off
/**
 * @file directory.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: directory.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/content/directory.php $
 */
// @formatter:on

class ActionControllerContentDirectory extends WCActionController {
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;
			global $wcDirectory;

			if(!$wcProfile->allowedTo(WC_PERM_DIRECTORY_VIEW)) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "You're not allowed to open directories";
			}
			if($check == WC_HTTPERROR_OK && (!$wcDirectory || !$wcDirectory->ok())) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_BAD_REQUEST;
				$errno = $check;
				$error = "Invalid directory";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;

		//
		// Loading global pointers.
		global $wcDirectory;
		global $wcProfile;

		$limiter = "directory";
		$limits = WCFVPager::GetLimitsFor($limiter);

		$this->_manager->PATH = "{$wcDirectory->name}";
		//
		// Sub-directories
		// @{
		$subdirectories = array();

		$has = false;
		foreach($wcDirectory->subDirectories() as $directory) {
			$has = true;
			$subdirectories[] = array(
				"id" => $directory->id,
				"name" => $directory->name
			);
		}
		if($has) {
			$this->_manager->info_subdirectories = $subdirectories;
		}
		// @}
		//
		// Parent directory.
		// @{
		$parent = $wcDirectory->parentDirectory();
		if($parent) {
			$this->_manager->info_parent_id = $parent->id;
			$this->_manager->info_parent_name = $parent->name;
		}

		if(isset($this->_manager->info_parent_id) || isset($this->_manager->info_subdirectories)) {
			$this->_manager->info_moreinfo = true;
		}
		// @}
		//
		// Loading comics list.
		// @{
		$showComics = false;
		$comics = array();
		$comicCount = 1;
		foreach($wcDirectory->comicsFull() as $comic) {
			if($comic->ok()) {
				if($comicCount >= $limits["min"] && $comicCount <= $limits["max"]) {
					$showComics = true;
					$comics[] = WCFVBoxes::GetComicPreviewAssigns($comic);
				}
				$comicCount++;
			}
		}
		$this->_manager->comics = $comics;
		if($showComics) {
			$this->_manager->show_comics = true;
		}
		// @}
		//
		// Loading images list.
		// @{
		$showImages = false;
		$images = array();
		$imagesCount = 1;
		$flowUri = WCFlowManager::I()->urlFlowFor($wcDirectory);
		$items = $wcDirectory->imagesFull();
		uasort($items, "WCImage::NameSort");
		foreach($items as $image) {
			if($image->ok()) {
				if($imagesCount >= $limits["min"] && $imagesCount <= $limits["max"]) {
					$showImages = true;
					$images[] = WCFVBoxes::GetImagePreviewAssigns($image, false, $flowUri);
				}
				$imagesCount++;
			}
		}
		$this->_manager->images = $images;
		if($showImages) {
			$this->_manager->show_images = true;
		}
		// @}
		if($wcProfile->allowedTo(WC_PERM_DIRECTORY_RENAME)) {
			$this->_manager->info_name_editable = true;
		}

		WCViewInfoFactory::LoadInfo($wcDirectory, true);
		WCViewInfoFactory::LoadInterfacesInfo($wcDirectory, true);
		//
		// Pager configuration.
		$itemsCount = max($this->_manager->info_count_comics, $this->_manager->info_count_images);
		WCFVPager::GetAssignsIntoManagerFor($itemsCount, $limiter);

		return $ok;
	}
}
